package com.chen.action;
import java.util.List;

import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.chen.pojo.User;
import com.chen.service.UserService;
import com.opensymphony.xwork2.ModelDriven;
@ParentPackage("json-default")
@AllowedMethods("login,register,findAll,edit,update")
@Results(@Result(type="json",params={
		"root","list"
	}
))
public class UserAction implements ModelDriven<User>{
	private static final long serialVersionUID = 1L;
	@Resource
	private UserService userService;
	private List<User> list;
	
	User user = new User();
	
	@Action(value="login",results={
			@Result(name="login",location="/page/index.jsp"),
			@Result(name="error",location="/page/main.jsp")
	})
	public String login(){
		List list = userService.login(user);
		if(list.size()>0){
			System.out.println("登录成功");
			return "login";
		}else{
			System.out.println("登录失败");
			return "error";
		}
	}
	@Action(value="register",results={
			@Result(name="register",location="/page/login.jsp"),
	})
	public String register(){
		userService.register(user);
		return "register";
	}
	@Action(value="findAll")
	public String findAll(){
		list = userService.findAll();
		return "success";
	}
	@Action(value="edit")
	public String edit(){
		list = userService.edit(user.getId());
		//System.out.println(list.get(0).getUsername());
		return "success";
	}
	@Action(value="update",results={
			@Result(name="update",location="/page/text1.jsp")
	})
	public String upDate(){
		userService.upDate(user);
		return "update";
		
	}
	public User getModel() {
		return user;
	}
	public List<User> getList() {
		return list;
	}
	public void setList(List<User> list) {
		this.list = list;
	}
}